package com.jackeysun.app_widget

import android.os.Build
import android.text.Selection
import android.text.Spannable
import android.text.method.LinkMovementMethod
import android.text.method.MovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.textclassifier.TextLinks.TextLinkSpan
import android.widget.TextView

/**
 *   Created by sunxunchao on 2021/12/30.
 */
class WordMovementMethod: /*BaseMovementMethod(),*/ MovementMethod {

    private val TAG = WordMovementMethod::class.java.simpleName
    override fun initialize(widget: TextView?, text: Spannable?) {
    }

    override fun onKeyDown(
        widget: TextView?,
        text: Spannable?,
        keyCode: Int,
        event: KeyEvent?
    ): Boolean {
        Log.d(TAG, "onKeyDown: $text")
        return false
    }

    override fun onKeyUp(
        widget: TextView?,
        text: Spannable?,
        keyCode: Int,
        event: KeyEvent?
    ): Boolean {
        Log.d(TAG, "onKeyUp: $text")
        return false
    }

    override fun onKeyOther(view: TextView?, text: Spannable?, event: KeyEvent?): Boolean {
        Log.d(TAG, "onKeyOther: $text")
        return false
    }

    override fun onTakeFocus(widget: TextView?, text: Spannable?, direction: Int) {
        Log.d(TAG, "onTakeFocus: $text")
    }

    override fun onTrackballEvent(
        widget: TextView?,
        text: Spannable?,
        event: MotionEvent?
    ): Boolean {
        Log.d(TAG, "onTrackballEvent: $text")
        return false
    }

    override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
        Log.d(TAG, "onTouchEvent: $buffer")
        val action = event.action

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            var x = event.x.toInt()
            var y = event.y.toInt()
            x -= widget.totalPaddingLeft
            y -= widget.totalPaddingTop
            x += widget.scrollX
            y += widget.scrollY
            val layout = widget.layout
            val line = layout.getLineForVertical(y)
            val off = layout.getOffsetForHorizontal(line, x.toFloat())
            val links: Array<ClickableSpan> = buffer.getSpans<ClickableSpan>(
                off, off,
                ClickableSpan::class.java
            )
            if (links.size != 0) {
                val link = links[0]
                if (action == MotionEvent.ACTION_UP) {
                        link.onClick(widget)
                } else if (action == MotionEvent.ACTION_DOWN) {
                    if (widget.context.applicationInfo.targetSdkVersion
                        >= Build.VERSION_CODES.P
                    ) {
//                        // Selection change will reposition the toolbar. Hide it for a few ms for a
//                        // smoother transition.
//                        widget.hideFloatingToolbar(LinkMovementMethod.HIDE_FLOATING_TOOLBAR_DELAY_MS)
                    }
                    Selection.setSelection(
                        buffer,
                        buffer.getSpanStart(link),
                        buffer.getSpanEnd(link)
                    )
                }
                return true
            } else {
                Selection.removeSelection(buffer)
            }
        }

        return false
    }

    override fun onGenericMotionEvent(
        widget: TextView?,
        text: Spannable?,
        event: MotionEvent?
    ): Boolean {
        Log.d(TAG, "onGenericMotionEvent: $text")
        return false
    }

    override fun canSelectArbitrarily(): Boolean {
        return false
    }


}